package com.bbcare.nutritious.plat.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.nutritious.plat.service.IActivityHuamiService;
import com.bbcare.nutritious.plat.service.IWeightService;

/**
 * 
 * @name:         ActivityHuamiController.java
 * @author:       tao
 * @createTime:   2017年11月6日
 * @description:  手环运动控制器    --无会话接口
 * @modify:
 *
 */
@Controller
@RequestMapping("/webapi/service/activity")
public class ActivitySleepController {
	
    public static final Logger logger = Logger.getLogger(ActivitySleepController.class);
    
    @Autowired
    private IActivityHuamiService activityHuamiService;
    
    @Autowired
    private IWeightService weightService;
    
    
    
    @ResponseBody
    @RequestMapping("/listSleep")
    public ResultModel<Object> getSleepListByUserId(@RequestBody String arg,HttpServletRequest request) throws Exception {
    	logger.info("in getSleepListByUserId----arg==" + arg);
        JSONObject jsonEsb = JSON.parseObject(arg);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
        String userId = requestParam.getString("userId");
        if(StringUtils.isEmpty(userId)){
        	logger.debug("out getSleepListByUserId----arg== null");
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，患者ID不能为空");
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        
        
        String startTime = requestParam.getString("startTime");
        
		//上传开始时间和 上传结束时间 校验
		if(StringUtils.isEmpty(startTime)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "开始时间不能为空");
		}
		String endTime = requestParam.getString("endTime");

		if(StringUtils.isEmpty(endTime)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "结束时间不能为空");
		}
        String tenantId = weightService.getUserTenantIdByUserId(userId);// 租户id
        if(StringUtils.isEmpty(tenantId)){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "患者信息不存在");
		}
        paramMap.put("tenantId",tenantId);
        paramMap.put("userId",userId);
        paramMap.put("type",Constants.HUAMI_TYPE_SLEEP);
        paramMap.putAll(requestParam);
        Map<String, Object> map= activityHuamiService.getHuamiDataListByUserId(paramMap);
        /*if(!CollectionUtils.isEmpty(list)){
        	familyReportService.getGrowthPatientCount(paramMap);
        }*/
		logger.info("out getSleepListByUserId----arg==" + map);
		return new ResultModel<Object>(map);
    }
    
    
}
